//
//      ===  Demo LCDSmartie Plugin for c#  ===
//
// dot net plugins are supported in LCD Smartie 5.3 beta 3 and above.
//

// You may provide/use upto 20 functions (function1 to function20).

using System;
using System.Diagnostics;
using iTunesLib;

namespace itunes2
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class LCDSmartie
	{
		private iTunesAppClass it = null;

		public LCDSmartie()
		{
			findAndInitiate();
		}


	
		// This function is used in LCDSmartie by using the dll command as follows:
		//    $dll(itunes2,1,<Command>,)
		// Smartie will then display on the LCD: 
		public string function1(string param1, string param2)
		{
			if(it == null)
			{
				if(findAndInitiate() != 0) 
				{
					if(param1 == "Stat")
						return "iTunes not started";
					else
						return "";
				}
			}
			//			string toReturn = "";// = "";			
			try 
			{

				switch(param1) 
				{
					case "Title":
						if(it.CurrentStreamTitle != null)
							return it.CurrentStreamTitle;
						return it.CurrentTrack.Artist + " - " + it.CurrentTrack.Name;
					case "KBPS":
						return it.CurrentTrack.BitRate.ToString();
					case "Freq":
						return (it.CurrentTrack.SampleRate/1000).ToString("00");
					case "Stat":
						return it.PlayerState.ToString().Substring(13).ToLower();
					case "Position":
						return position(param2);
					case "Polo":
						return sec2hrsminsec(it.PlayerPosition,false);
					case "Posh":
						return sec2hrsminsec(it.PlayerPosition,true);
					case "pos":
						return it.PlayerPosition.ToString("0");
					case "Time":
						return sec2min( it.PlayerPosition );
					case "Relo":
						return sec2hrsminsec(it.CurrentTrack.Duration-it.PlayerPosition, false);
					case "Resh":
						return sec2hrsminsec(it.CurrentTrack.Duration-it.PlayerPosition,true);					
					case "Rem":
						return (it.CurrentTrack.Duration-it.PlayerPosition).ToString();	
					case "Lengtl":
						return sec2hrsminsec(it.CurrentTrack.Duration,false);
					case "Lengts":
						return sec2hrsminsec(it.CurrentTrack.Duration,true);
					case "Length":
						return it.CurrentTrack.Duration.ToString("0");
					case "Tracknr":
						return it.CurrentTrack.Index.ToString("0");
					case "TotalTracks":
						return it.CurrentPlaylist.Tracks.Count.ToString("0");
					case "PlayList":
						return it.CurrentPlaylist.Name;

					case "Channels":
					default:
						return "Not implemented (yet)";
				}
			} 
			catch (Exception e) 
			{
				if( e.Message.IndexOf("RPC server is unavailable")> -1 )
					it = null;
				if(param1 == "Stat")
					return "iTunes not started";
				return "";
			}
		}
		
		private int findAndInitiate()
		{
			Process[] myProcesses = Process.GetProcesses();
			foreach(Process myProcess in myProcesses)
			{
				if (myProcess.ProcessName.Equals("iTunes"))
				{
					it = new iTunesAppClass();
			//		this.it.OnAboutToPromptUserToQuitEvent += new iTunesLib._IiTunesEvents_OnAboutToPromptUserToQuitEventEventHandler(this.iTunesExit);
					return 0;
				}
			
			}
			return -1;
		}
		/*
		public string function1(string param1, string param2)
		{
			if(it == null)
			{
				if(findAndInitiate() != 0) 
				{
					if(param1 == "Stat")
						return "iTunes not started";
					else
						return "";
				}
			}
			//			string toReturn = "";// = "";			
			try 
			{
				it.CurrentVisual.Name;


			} 
			catch ( Exception ) {}
		}
	/*	private void iTunesExit() //implements _IiTunesEvents_OnAboutToPromptUserToQuitEventEventHandler
		{
			it.Quit();
			it = null;
		}
	/*{
			string s = "";
			Process[] myProcesses = Process.GetProcesses();
			foreach(Process myProcess in myProcesses)
			{
				s += myProcess.ProcessName + ";";
			}
			return s;
		}
*/

		

		//
		// Define the minimum interval that a screen should get fresh data from our plugin.
		// The actual value used by Smartie will be the higher of this value and of the "dll check interval" setting
		// on the Misc tab.  [This function is optional, Smartie will assume 300ms if it is not provided.]
		// 
		public int GetMinRefreshInterval()
		{
			return 500; // 300 ms (around 3 times a second)
		}

		private string sec2hrsminsec(int secs, bool shortFormat)
		{
			int hrs = (secs / 3600);
			int min = (secs / 60)-(hrs * 60);
			int sec = secs-(min*60)-(hrs * 3600);

			if(shortFormat)
			{
				if(hrs > 0)
					return hrs.ToString("#0:") + min.ToString("00:") + sec.ToString("00");
				if(min > 0)
					return min.ToString("#0:")+ sec.ToString("00");
				return sec.ToString();
			}
				if(hrs > 0)
					return hrs.ToString() + "hrs " + min.ToString("00") + "min " +
						sec.ToString("00") + "sec";
				if(min > 0)
					return min.ToString("#0") + "min " +
						sec.ToString("00") + "sec";
				return sec.ToString() + "sec";
		}

		private string sec2min(int sec)
		{
			int minutes = (sec / 60);
			int seconds = sec-(minutes*60);
			return minutes.ToString("#0") + ":"+ seconds.ToString("00");
		}

		private string position(string s)
		{
            float lenght = it.CurrentTrack.Duration, curpos = it.PlayerPosition;
			int len = int.Parse(s);
			string toReturn = "";
			for(int i=0;i<(len-1);i++)
				toReturn += "-";
			//lenght /= len;
			//curpos /= len;
		//	toReturn += lenght.ToString() + " "+ curpos.ToString() + " " + ((curpos/lenght)).ToString();
			toReturn = toReturn.Insert((int)(len*(curpos/lenght)),"+");
			return toReturn.ToString();
		}
	}
}

